package com.cby.operation.anti.js

import android.content.Context
import android.webkit.JavascriptInterface
import com.cby.operation.anti.AntiAddictManager
import com.cby.operation.anti.AntiHeartManager
import com.cby.operation.common.ui.UIStackManager
import com.cby.operation.common.util.CbyLogUtils
import com.cby.operation.common.util.HandlerUtils
import com.cby.operation.sdk.TAG_FRAGMENT_ANTI
import com.cby.operation.sdk.TAG_FRAGMENT_PAY_LIMIT
import com.cby.operation.sdk.TAG_FRAGMENT_REAL_NAME
import com.cby.operation.sdk.js.BaseJsInterface

/**
 * @description: RealNameJsInterface
 *
 * @author ChenBaoyang
 * @date 8/2/21 11:05
 */
class RealNameJsInterface(private val context: Context) : BaseJsInterface(context) {

    private val tag = "RealNameJsInterface"

    /**
     * h5实名认证提交结果返回
     * @param code
     */
    @JavascriptInterface
    fun onRealNameCallback(code: Int, msg: String) {
        CbyLogUtils.flog(tag, "onRealNameCallback= $code===msg=$msg")
        onAuthNameCallBack(code, msg)
    }

    private fun onAuthNameCallBack(code: Int, msg: String?) {
        HandlerUtils.runOnMainThread {
            UIStackManager.instance!!.finishFragment(TAG_FRAGMENT_ANTI)
            UIStackManager.instance!!.finishFragment(TAG_FRAGMENT_REAL_NAME)
            UIStackManager.instance!!.finishFragment(TAG_FRAGMENT_PAY_LIMIT)
            AntiAddictManager.instance.onBeat(AntiHeartManager.instance.currentSecond)
        }
    }

    /**
     * h5实名认证关闭弹窗
     * @param code
     */
    @JavascriptInterface
    fun onRealNameClose(code: Int, msg: String?) {
        HandlerUtils.runOnMainThread {
            UIStackManager.instance!!.finishFragment(TAG_FRAGMENT_ANTI)
            AntiAddictManager.instance.onBeat(AntiHeartManager.instance.currentSecond)
        }
    }
}